package com.sakura.probe.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakura.probe.common.api.CommonResult;
import com.sakura.probe.common.util.HttpServletRequestUtil;
import com.sakura.probe.pojo.PersonInfo;
import com.sakura.probe.service.PersonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Api(tags = "AdminPersonController",description = "后台用户管理的相关操作")
@Controller
@Slf4j
@RequestMapping("/backstage/person")
public class AdminPersonController {
    @Autowired
    private PersonInfoService personService;

    @ApiOperation("根据搜索条件分页显示用户列表")
    @PostMapping("/listByCondition")
    @ResponseBody
    @RequiresRoles(logical = Logical.OR,value = {"1","2"})
    public CommonResult getPersonList(HttpServletRequest request,Long pageIndex,Long pageSize,String name){
        log.info("token="+request.getHeader("Token"));
        QueryWrapper<PersonInfo> query = (null==name||"".equals(name))?null:new QueryWrapper<PersonInfo>().like("username", name);
        IPage<PersonInfo> page = personService.page(new Page<PersonInfo>()
                .setCurrent(pageIndex).setSize(pageSize), query);
        return CommonResult.success(page);
    }

    @ApiOperation("查看用户的基本信息")
    @GetMapping("/showPersonInfo")
    @ResponseBody
    public CommonResult showPersonInfo(Long userId){
        return CommonResult.success(personService.getById(userId));
    }

    @ApiOperation("修改用户的状态值")
    @GetMapping("/updateStatus")
    @ResponseBody
    public CommonResult updatePersonStatus(Long userId,Integer status){
        PersonInfo person = new PersonInfo();
        person.setUserId(userId);
        person.setStatus(status);
        boolean result = personService.updateById(person);
        return result?CommonResult.success("update success"):CommonResult.failed();
    }

    @ApiOperation("删除单个用户的信息")
    @GetMapping("/delPerson")
    @ResponseBody
    public CommonResult deletePerson(Long userId){
        return personService.deletePerson(userId)?CommonResult.success("删除成功"):CommonResult.failed();
    }

    @ApiOperation("批量删除用户的信息")
    @GetMapping("/delBatchPerson")
    @ResponseBody
    public CommonResult deleteBatchPerson(HttpServletRequest request){
        //获取要删除的员工id列表
        List<Long> list = HttpServletRequestUtil.getLongList(request, "id", 10);
        return personService.batchDeletePerson(list)?CommonResult.success("批量删除成功"):CommonResult.failed();
    }

    //获取要删除的员工id,并加入list集合中
//    private List<Long> getListFromRequest(HttpServletRequest request) {
//        List<Long> ans = new ArrayList<Long>();
//        for (int i=0;i<10;i++){
//            Long id = HttpServletRequestUtil.getLong(request,"id"+i);
//            if (id!=-1L){
//                ans.add(id);
//            }else {
//                break;
//            }
//        }
//        return ans;
//    }

}
